package com.flashflexpro.flexdirector.adapter.codec {
import com.flashflexpro.flexdirector.adapter.descriptor.DescPropertyAuto;
import com.flashflexpro.flexdirector.model.CodeStringValue;

import mx.automation.IAutomationObject;

public class CodecAsset extends CodecDefault{

    override public function encode(obj:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject):CodeStringValue{
        var rt:CodeStringValue = new CodeStringValue();
        rt.desc = descProperty.getDto();
        rt.value = getMemberFromObject( obj, descProperty );

		if (rt.value != null){
	        var str:String = rt.value.toString();

	        str = stripNoiseFromString("css_", String(str));
	        str = stripNoiseFromString("embed_mxml_", String(str));
	        str = stripNoiseFromString("embed_as_", String(str));
		}
        rt.code = rt.string = str;

        return rt;
    }

    override public function decode(obj:Object, value:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject, inString:Boolean = false):void {
        throw new Error("Resource is not Settable");
    }


    private function stripNoiseFromString(beginPart:String, asset:String):String{
	    var pos:int = asset.indexOf(beginPart);

	    if (pos != -1)
	    {
	        asset = asset.substr(pos + beginPart.length);

	        var lastUnderscorePos:int = asset.lastIndexOf("_");

	        if (lastUnderscorePos != -1)
	        {
	            asset = asset.substr(0, lastUnderscorePos);
	        }
	    }

	    return asset;
    }
}
}